package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import obj.Path;

public class VehicleReportPanel extends JPanel {

	private static final long serialVersionUID = 8324429661412676641L;

	private static JTable table;
	private DefaultTableModel model;

	private static Boolean isTableDrawn = Boolean.FALSE;

	public VehicleReportPanel() {
		String colNames[] = { "Path Name", "Vehicles Entered", "Vehicles Exited", "Vehicles on Screen", "Avg Speed" };
		model = new DefaultTableModel(colNames, 0);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		table = new JTable(model);
		this.add(table);
		table.setGridColor(Color.LIGHT_GRAY);
		table.setBackground(Color.WHITE);
		table.setBorder(null);
		table.setPreferredScrollableViewportSize(new Dimension(650, 170));
		table.setFillsViewportHeight(true);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(null);
		this.add(scrollPane);

	}

	public void drawTable(List<Path> pList) {
		if (!isTableDrawn) {
			for (Path p : pList) {
				model.addRow(new Object[] { p.getName(), "", "", "", "" });
			}
		}

		isTableDrawn = Boolean.TRUE;
	}

	public static void refreshTable(Path p) {
		int rows = table.getRowCount();
		int cols = table.getColumnCount();
		if (p != null) {

			for (int i = 0; i < rows; i++) {
				if (table.getModel().getValueAt(i, 0).equals(p.getName())) {
					table.getModel().setValueAt(p.getVehiclesEntered().size(), i, 1);
					table.getModel().setValueAt(p.getVehiclesExited().size(), i, 2);
					table.getModel().setValueAt(p.getVehiclesEntered().size() - p.getVehiclesExited().size(), i, 3);
					table.getModel().setValueAt(p.getMaxSpeed(), i, 4);
					
				}
			}
		}

	}
}
